package com.smartlink.app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.smartlink.app.R;
import com.smartlink.app.common.Constant;
import com.smartlink.app.controller.LoginController;
import com.smartlink.app.dao.ClassRoomInfoDao;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class BaseActivity extends AppCompatActivity {

    private SharedPreferences preferences;
    private static final String TAG = "BaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        preferences = getSharedPreferences("main", MODE_PRIVATE);
        // initJpush();
        downloadClassRoomInfo();
        //自动登陆
        final String username = preferences.getString("number", "");
        final String password = preferences.getString("password", "");

        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            LoginController loginController = new LoginController(BaseActivity.this);
            loginController.login(username, password, null);
        } else {
            Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
            intent.putExtra("error", "请登录");
            startActivity(intent);

        }
        BaseActivity.this.finish();
    }

    /**
     * 设置极光推送相关信息
     */
    private void initJpush() {
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

    /**
     * 下载教室数据
     */
    private void downloadClassRoomInfo() {
        AsyncHttpClient client = new AsyncHttpClient();
        String ip = preferences.getString("ip", Constant.HTTP.HTTP_PORT);
        String url = "http://" + ip + ":8080/smartlink/downloadClassRoomInfo";
        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String body = new String(bytes);
                Log.i(TAG, "onSuccess: " + body);
                try {
                    JSONObject jsonObject = new JSONObject(body);
                    int resultCode = jsonObject.getInt("result_code");
                    if (resultCode == 0) {
                        JSONArray array = jsonObject.getJSONArray("result");
                        Gson gson = new Gson();
                        List<ArrayList> list = gson.fromJson(array.toString(), ArrayList.class);
                        if (list.size() > 0) {
                            new ClassRoomInfoDao(BaseActivity.this).insertClassRoomInfo(list);
                        }
                        Log.i(TAG, "onSuccess: " + list.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
}
